<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/workflow/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/workflow/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <span class="layui-card-header"><i class="layui-icon layui-icon-form"></i>&nbsp;待办任务列表</span>
            </div>
        </script>

        <fieldset class="layui-elem-field site-demo-button" style="margin-top: 30px;">
            <legend>操作</legend>
            <div>
                <button id="addLeave"  type="button" class="layui-btn">添加申请</button>
                <button id="goWorkflowPage" type="button" class="layui-btn layui-btn-normal">跳转到工作流审批管理界面</button>
            </div>
        </fieldset>

        <table class="layui-hide" id="ToDoTasksTableId" lay-filter="currentTableFilter"></table>
        <!--分页-->
        <div class="layui-table-page layui-border-box">
            <div id="barcon" class="page_div" name="barcon"></div>
        </div>

        <script type="text/html" id="currentTableBar">
            {{# if(d.status === 1){ }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-handle" lay-event="submitApplication">提交申请</a>
            {{# } else { }}
            <a class="layui-btn layui-btn-xs layui-btn-normal data-count-handle" lay-event="viewApplication">查看流程</a>
            {{# } }}
        </script>

    </div>
</div>
<script src="/workflow/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/workflow/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="/workflow/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/workflow/js/ManualPaging.js"></script>
<script>

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var insTb = table.render({
            elem: '#ToDoTasksTableId',
            toolbar: '#toolbarDemo',
            defaultToolbar: [''],
            url: window.location.origin+'/leave/list',
            parseData: function(res){ //res 即为原始返回的数据
                //处理时间
                for(var i = 0 ; i< res.obj.length ; i++){
                    res.obj[i]["startDate"] = renderTime(res.obj[i].startDate);
                }
                return {
                    "code": res.status, //解析接口状态,默认成功是0
                    "msg": res.msg, //解析提示文本
                    "count": res.obj.length, //解析数据长度
                    "data": res.obj //解析数据列表
                };
            },
            cols: [[
                {field: 'id', width:'10%', title: 'ID',align:"center"},
                {field: 'username', width:'10%', title: '用户',align:"center"},
                {field: 'reason', width:'40%', title: '请假原因',align:"center"},
                {field: 'days', width:'5%', title: '请假天数',align:"center"},
                {field: 'startDate', title: '开始时间', width:'15%',align:"center"},
                {field: 'statusText', width:'10%', title: '状态',align:"center"},
                {title: '操作', width:'10%', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: false,
            skin: 'line',
            // id:'ToDoTasksTableId',
            done:function (res, curr, count) {
                if(res.code==1){
                    layer.alert(res.msg+'<br>'+res.data, {
                        icon: 2,
                        title:"提示"
                    });
                }

                if(count>0){
                    goPage(1);
                }


            }
        });

        /**
         * toolbar监听事件
         */
        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === "submitApplication" && obj.data.status === 1) {
                var loadIndex = layer.load(2);
                $.post( window.location.origin+`/activiti/startProcess`,
                    {
                        processDefinitionKey:'LeaveApplication',
                        instanceName:obj.data.username+"请假申请"+obj.data.days+"天",
                        businessKey:obj.data.id
                    }, function (res) {
                        layer.close(loadIndex);
                        if (res.status === 0) {
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.alert(res.msg+'<br>'+res.obj, {icon: 2});
                        }
                        insTb.reload();
                    });
            }else if(obj.event === "viewApplication" ){
                $.getJSON(window.location.origin+`/processInstance/getInstancesByInstId?instId=${obj.data.instId}`,(res)=>{
                    let data = res.obj;
                    window.open('/bpmnjs/dist/index.html?type=lookBpmn&instanceId='+data.id+'&deploymentFileUUID='+data.deploymentId+'&deploymentName='+ encodeURI(data.resourceName)+'');
                });
            }
        });

        function renderTime(date) {
            var dateee = new Date(date).toJSON();
            return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
        }
        $("#goWorkflowPage").on('click',()=>{
            window.open('/workflow/index.html');
        });
        $("#addLeave").on('click',()=>{
            layer.prompt({
                title: '请输入请假天数,原因',
                formType: 0 // 0：文本输入框，1：密码输入框
            }, function(value, index, elem){
                // value 为输入的内容
                $.post( '/leave/add', {
                    "days":value.split(',')[0],
                    "reason":value.split(',')[1]
                }, function (res) {

                   /* table.reload('ToDoTasksTableId', {
                        page: { curr: 1 }
                    });*/
                    insTb.reload();
                });
                layer.close(index); // 关闭弹出框
            });
        })
    });
</script>

</body>
</html>